'''last update @2025-02-03
  添加录像指定功能
'''

from datetime import datetime
import os
import shutil

from mameRoms import *

### 常量定义
MAME = r'mame.exe'
PARAMS = '-plugin autofire,inputmacro,cheat,cheatfind -window -keyboardprovider win32'
PARAMS += r' -console'
#PARAMS += r' -language Chinese_Simplified -console'

### 变量定义
inpText = '' # 接受输入的字符层
index = 0 # 输入编号
romName = '' # 选择的rom文件
command = '' # 命令行指令
needReplay = False # 是否录像
now = '' # 当前时间字符窜
recordFile = '' # 录像文件名,用于命令行参数

### 脚本执行
print('执行MAME0.273版本:')
while((index > -1) and (index < len(ROMS))):
  ## 初始化
  needReplay = False
  now = datetime.now().strftime('%Y%m%d_%H%M%S')
  recordFile = 'temp.inp' # 默认录像文件
  print('当前时间:', now)
  ## 提示信息显示
  i = 0 # 临时变量
  while (i < len(ROMS)):
    if (i == len(ROMS)-1):
      print(f'{i+1:3} : {ROMS[i][1]:20}')
    else:
      print(f'{i+1:3} : {ROMS[i][1]:20}   {i+2:3} : {ROMS[i+1][1]:20}')
    i += 2
  ## 输入ROM编号,回车退出
  try:
    print('')
    inpText = input("输入rom编号并按回车执行,直接回车退出:")
    if (inpText in ROMS_SHOTCUT):
      romName = ROMS_SHOTCUT[inpText]
    else:
      index = int(inpText)-1
      romName = ROMS[index][0]
  except:
    exit(1)
  ## 输入录像要求,把nvram文件夹也要复制一份!
  try:
    index = int(input('需要专门录像输入1,临时录像输入2,不录像输入其他:'))
    if (index == 1):
      needReplay = True
      recordFile = romName+'_'+now+'.inp'
      shutil.copytree(f'nvram\{romName}', 
        f'nvram\{recordFile}', dirs_exist_ok=True)
    if (index == 2):
      needReplay = True
      shutil.copytree(f'nvram\{romName}', 
        f'nvram\{recordFile}', dirs_exist_ok=True)
  except:
    pass
  ## 执行mame命令行
  if (needReplay):
    command = f' {MAME} {romName} {PARAMS} -record {recordFile}'
  else:
    command = f' {MAME} {romName} {PARAMS}'
  ## 启动脚本
  if romName in ROMS_BOOT:
    command += f' -autoboot_script .\\myLua\\autoboot\\{romName}.lua'
  os.system(command)
  print('\n')
  
## 结束
exit(0)
